package com.estronger.shopmanage.ui.common;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.DensityUtils;

/**
 * 查看大图的界面，
 * 
 * @author Administrator
 * 
 */
public class ViewActivity extends BaseActivity {

	private ArrayList<ImageView> mImageViewList;
	private ViewPager vpGuide;
	private LinearLayout llPointGroup;
	private int mPointWidth;
	private View viewRedPoint;
	private TextView guide_start;
	private ArrayList<String> imgs = new ArrayList<String>();
	private boolean isLocal;

	@Override
	protected void init() {
		isLocal = getIntent().getBooleanExtra("isLocal", false);
		imgs.addAll(getIntent().getStringArrayListExtra("imgs"));
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_guide);

		vpGuide = (ViewPager) findViewById(R.id.vp_guide);
		llPointGroup = (LinearLayout) findViewById(R.id.ll_point_group);
		viewRedPoint = findViewById(R.id.view_red_point);
		guide_start = (TextView) findViewById(R.id.guide_start);
		guide_start.setVisibility(View.GONE);
		vpGuide.setAdapter(new GuideAdapter());
		vpGuide.setOnPageChangeListener(new GuidePageListener());
		initViews();

		mImageViewList = new ArrayList<ImageView>();

		for (int i = 0; i < imgs.size(); i++) {
			ImageView image = new ImageView(this);
			if (isLocal) {
				Bitmap bitmap = BitmapFactory.decodeFile(imgs.get(i));
				image.setImageBitmap(bitmap);
				image.setScaleType(ScaleType.FIT_CENTER);
			} else {
				MyApplication.imageLoader.displayImage(
						NetManager.getDownloadURL() + "?filepath="
								+ imgs.get(i), image);
			}
			mImageViewList.add(image);
		}
	}

	private void initViews() {

		for (int i = 0; i < imgs.size(); i++) {
			View point = new View(this);
			point.setBackgroundResource(R.drawable.shape_point_gray);

			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					DensityUtils.dp2px(this, 10), DensityUtils.dp2px(this, 10));
			if (i > 0) {
				params.leftMargin = DensityUtils.dp2px(this, 10) * 2;
			}

			point.setLayoutParams(params);

			llPointGroup.addView(point);
		}

		llPointGroup.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						llPointGroup.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
						if (null != llPointGroup.getChildAt(1)) {
							mPointWidth = llPointGroup.getChildAt(1).getLeft()
									- llPointGroup.getChildAt(0).getLeft();
						}
					}
				});
	}

	class GuideAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return imgs.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public ImageView instantiateItem(ViewGroup container, int position) {
			container.addView(mImageViewList.get(position));
			return mImageViewList.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}

	class GuidePageListener implements OnPageChangeListener {

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {

			int len = (int) (mPointWidth * positionOffset) + position
					* mPointWidth;

			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewRedPoint
					.getLayoutParams();
			params.leftMargin = len;

			viewRedPoint.setLayoutParams(params);
		}

		@Override
		public void onPageSelected(int position) {
		}

		@Override
		public void onPageScrollStateChanged(int state) {
		}

	}

}
